package com.controller;


import com.bean.Teacher;
import com.bean.User;
import com.dao.TeacherMapper;
import org.springframework.stereotype.Controller;
import org.springframework.test.context.ContextConfiguration;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import javax.annotation.Resource;
import java.util.List;

@ContextConfiguration("classpath:springmvc.xml")
@Controller
public class TeacherController {
    @Resource(name = "teacherMapper")
    private TeacherMapper teacherMapper;

    //登录
    @RequestMapping("/login")
    public String checklogin(Model model, User user) {
        User user2 = teacherMapper.checklogin(user);
        if (user2 != null) {
            return "redirect:/tomain";
        } else {
            model.addAttribute("msg", "输入账号或密码错误");
            return "index";
        }
    }

    //主界面
    @RequestMapping("/tomain")
    public String tomain(Model model) {
        List<Teacher> list = teacherMapper.queryAllTea();
        model.addAttribute("list", list);
        return "main";
    }

    //修改
    @RequestMapping("/toupdate")
    public String toupdate(Model model, int id) {
        Teacher tea = teacherMapper.queryTeaById(id);
        model.addAttribute("tea", tea);
        return "toupdate";
    }

    @RequestMapping("/doupdate")
    public String douodate(Model model, Teacher teacher) {
        int i = teacherMapper.updateTea(teacher);
        if (i > 0) {
            return "redirect:/tomain";
        } else {
            return "redirect:/toupdate";
        }
    }

    //删除
    @RequestMapping("/delete")
    public String delete(Model model, int id) {
        teacherMapper.deleteTea(id);
        return "redirect:/tomain";
    }

    //新增
    @RequestMapping("/toinsert")
    public String toinsert(Model model) {
        return "toinsert";
    }

    @RequestMapping("/doinsert")
    public String doinsert(Model model, Teacher teacher) {
        int i = teacherMapper.insertTea(teacher);
        if (i > 0) {
            return "redirect:/tomain";
        } else {
            return "redirect:/toinsert";
        }
    }

    //搜索
    @RequestMapping("/search")
    public String queryTeaByLike(Model model, String name, String subject) {  //��mybatis�ﴫ��������ʱ��������dao�����@Param�������Ĳ������ͰѲ�����װ��bean
        String nameString = "%" + name + "%";
        String subjectString = "%" + subject + "%";
        List<Teacher> list = teacherMapper.queryTeaByLike(nameString, subjectString);
        model.addAttribute("list", list);

        return "main";

    }

}
